<!DOCTYPE HTML>
<html lang="zh" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header">
</head>

<link rel="stylesheet" th:href="@{/css/todostyle.css}"/>
<style type="text/css">
    .table {
        table-layout: fixed;
    }
</style>
<body class="gray-bg">

<div class="container-div">
    <input id="typeYB" th:value="${type}" type="hidden"/>
    <div class="row">
        <form class="form-horizontal m" id="form-kaoping">
            <input id="id" name="id" th:value="${approver.id}" type="hidden">
            <input id="roleId" name="roleId" th:value="${roleId}" type="hidden"><!-- 待办人员角色 -->
            <input id="tableId" name="tableId" th:value="${approver.tableId}" type="hidden">
            <input id="approveorder" name="approveorder" th:value="${approver.approveorder}" type="hidden">
        </form>
        <div class="col-sm-12 select-table table-striped">
            <table data-mobile-responsive="true" id="bootstrap-table"
                   style="word-break:break-all; word-wrap:break-all;"></table>
        </div>
        <div class="evaluate_b_but" style="text-align: center;">
            <!--<button onclick="doSubmit('BC')">保存</button>-->
            <button onclick="doSubmit('TJ')" th:if="${type!=1}">提交</button>
            <button onclick="appealfun()" th:if="${type==1}">申诉</button>
            <button onclick="closeItem()">取消</button>
            <!--<button onclick="getDate()">得到数据</button>-->
        </div>
    </div>
</div>
<div th:include="include :: footer"></div>
<script th:src="@{/js/fileinput.min.js}"></script>
<script th:src="@{/js/fileinput.js}"></script>
<script th:src="@{/js/zh.js}"></script>
<script th:inline="javascript">
    var prefix = ctx + "zzjx/pefctasstable";

    $(function () {
        var datas = {};
        var id = [[${approver.id}]];
        var tableId = [[${approver.tableId}]]; //表单id
        var approverCode = [[${approver.approverCode}]];
        if ($("#typeYB").val() == 1) {
            datas = {"tableId": tableId, "id": id, "approverCode": approverCode};
        } else {
            datas = {"tableId": tableId, "id": id};
        }
        if (tableId != "") {
            $.modal.loading("请等待加载预览信息!");
            $.ajax({
                url: prefix + "/previewForLdzhpj"
                , data: datas
                , dataType: "JSON"
                , type: "GET"
                , async: false
                , success: function (result) {
                    $.table.destroy("bootstrap-table");
                    buildTable($('#bootstrap-table'), result);
                    $.modal.closeLoading();
                }, error: function () {
                    $.modal.closeLoading();
                    $.modal.alertError("获取预览信息失败!");
                }
            })
            return false;
        }
        if ($("#typeYB").val() == 1) {
            $('input[type="text"]').attr('disabled', 'disabled');
            $('input[type="radio"]').attr('disabled', 'disabled');
            $('textarea').attr('disabled', 'disabled');
            $('select').attr('disabled', 'disabled');
        }
    });


    /**
     * 构建预览信息表格
     * @param $el        table对象
     * @param list        行数据
     */
    function buildTable($el, list) {
        var isD = "";
        if ($("#typeYB").val() == 1) {
            isD = "disabled";
        }
        var columns = [
            [{
                field: "id",
                title: "ID",
                visible: false,
                rowspan: 2,
                colspan: 1
            }, {
                field: 'indName',
                title: '被评价部门',
                rowspan: 2,
                colspan: 1,
                align: 'center',
                halign: 'center',
                width: 10
            }, {
                field: 'target1',
                title: '满意度得分',
                rowspan: 2,
                colspan: 1,
                align: 'center',
                halign: 'center',
                width: 10
            }, {
                field: 'gzjhTableId',
                title: '重点工作评价',
                rowspan: 2,
                colspan: 1,
                align: 'center',
                halign: 'center',
                width: 12,
                formatter: function (value, row, index) {
                    if (value == "" || typeof (value) == "undefined") {
                        return "";
                    } else {
                        return '<a  onclick="viewResult(\'' + value + '\',\'zdgz\')" >查看</a>';
                    }
                }
            }, {
                field: 'fileList',
                title: '年度工作总结',
                halign: 'center',
                rowspan: 2,
                colspan: 1,
                width: 17,
                formatter: function (value, row, index) {
                    if (value == "" || typeof (value) == "undefined") {
                        return "";
                    } else {
                        var html = ""
                        var l = value.length;
                        if (l > 0) {
                            for (var i = 0; i < l; i++) {
                                html += "<a onclick='checkFile(\"" + value[i].filePath + "\")'>" + value[i].fileName + "</a>";
                                html += "<br>";
                            }
                        }
                        return html;
                    }
                }
            }, {
                field: 'fmqdKf',
                title: '负面清单扣分',
                rowspan: 2,
                colspan: 1,
                align: 'center',
                halign: 'center',
                width: 10/*,
                    formatter: function (value, row , index) {
                        var id = row.id;
						var projectId = row.projectId;
                        return '<a  onclick="viewResult2(\''+projectId+'\',\'fmqd\')" >查看</a>';
                    }*/
            }, {
                title: '综合评价',
                align: 'center',
                colspan: 5,
                rowspan: 1,
                width: 32
            }],
            [{
                field: 'resultYx',
                title: '优秀',
                align: 'center',
                formatter: function (value, row, index) {
                    debugger
                    var name = "result" + row.id;
                    if (row.target == "5") {
                        return "<input class='required' type='radio' " + isD + " onclick='updateDate(" + index + ",5)' value='5' name='" + name + "' checked>";
                    } else {
                        return "<input class='required' type='radio' " + isD + " onclick='updateDate(" + index + ",5)' value='5' name='" + name + "'>"
                    }
                }
            }, {
                field: 'resultLh',
                title: '良好',
                align: 'center',
                formatter: function (value, row, index) {
                    var name = "result" + row.id;
                    if (row.target == "4.5") {
                        return "<input class='required' type='radio' " + isD + " onclick='updateDate(" + index + ",4.5)' value='4.5' name='" + name + "' checked>";
                    } else {
                        return "<input class='required' type='radio' " + isD + " onclick='updateDate(" + index + ",4.5)' value='4.5' name='" + name + "'>"
                    }
                }
            }, {
                field: 'resultYb',
                title: '一般',
                align: 'center',
                formatter: function (value, row, index) {
                    var name = "result" + row.id;
                    if (row.target == "4") {
                        return "<input class='required' type='radio' " + isD + " onclick='updateDate(" + index + ",4)' value='4' name='" + name + "' checked>";
                    } else {
                        return "<input class='required' type='radio' " + isD + " onclick='updateDate(" + index + ",4)' value='4' name='" + name + "'>"
                    }
                }
            }, {
                field: 'resultDgj',
                title: '待改进',
                align: 'center',
                formatter: function (value, row, index) {
                    var name = "result" + row.id;
                    if (row.target == "3") {
                        return "<input class='required' type='radio' " + isD + " onclick='updateDate(" + index + ",3)' value='3' name='" + name + "' checked>";
                    } else {
                        return "<input class='required' type='radio' " + isD + " onclick='updateDate(" + index + ",3)' value='3' name='" + name + "'>"
                    }
                }
            }, {
                title: '不合格',
                field: 'resultBhg',
                align: 'center',
                formatter: function (value, row, index) {
                    var name = "result" + row.id;
                    if (row.target == "2") {
                        return "<input class='required' type='radio' " + isD + " onclick='updateDate(" + index + ",2)' value='2' name='" + name + "' checked>";
                    } else {
                        return "<input class='required' type='radio' " + isD + " onclick='updateDate(" + index + ",2)' value='2' name='" + name + "'>"
                    }
                }
            }, {
                field: 'target',
                title: '结果',
                align: 'center',
                visible: false,
            }]
        ];

        //构建表格
        $el.bootstrapTable('destroy').bootstrapTable({
            columns: columns,
            data: list,
            search: false,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            showExport: false,                     //是否显示导出
            height: 600,
            pagination: false,
            modalName: "考核指标"
        });
    }

    /**
     * 查看
     * @param id
     * @param type
     */
    function ViewResult2(id, type) {
        console.log(id);
        console.log(type);
        var tableId = [[${approver.tableId}]]; //表单id

        //根据type和当前方案id，得到所需展示的方案id
        $.ajax({
            url: ctx + "zzjx/pefassproject/getViewProjectId",
            type: "GET",
            data: {"id": id, "type": type},
            success: function (result) {
                //根据类型，跳转不同查看页面
                /*var url = ctx + "zzjx/pefassproject/toMydpjtable/"+result;
                var title =  "满意度评价";
                openFull(title , url);*/
                if (type == "fmqd") {

                }
            }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                /*alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);*/
                $.modal.alertError("获取数据失败！");
                console.log("失败了");
                return false;
            }
        })
    }

    /**
     * 查看
     * @param id
     * @param type
     */
    function viewResult(id, type) {
        if (type == "zdgz") {
            var url = prefix + "/detail/" + id + "/0";
            openFull("重点工作表单详情", url);
        }
    }


    function updateDate(index, value) {
        var row = $("#bootstrap-table").bootstrapTable('getOptions').data[index];
        row["target"] = value + "";
    }

    //得到表的数据
    function getDate() {
        alert(JSON.stringify($("#bootstrap-table").bootstrapTable('getData')));
        console.log($("#bootstrap-table").bootstrapTable('getData'));
    }

    // 弹出层全屏
    openFull = function (title, url, width, height) {
        //如果是移动端，就使用自适应大小弹窗
        if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
            width = 'auto';
            height = 'auto';
        }
        if ($.common.isEmpty(title)) {
            title = false;
        }
        ;
        if ($.common.isEmpty(url)) {
            url = "/404.html";
        }
        ;
        if ($.common.isEmpty(width)) {
            width = 800;
        }
        ;
        if ($.common.isEmpty(height)) {
            height = ($(window).height() - 50);
        }
        ;
        var index = layer.open({
            type: 2,
            area: [width + 'px', height + 'px'],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: title,
            content: url,
            btn: ['关闭'],
            // 弹层外区域关闭
            shadeClose: true,
            cancel: function (index) {
                return true;
            }
        });
        layer.full(index);
    }

    function downloadFile(id, name) {
        window.location.href = ctx + "zzjx/projectSysfile/downloadFile?filePath=" + id + "&fileName=" + name;
    }


    function checkTableRequired() {
        var isOk = true;
        $("#bootstrap-table .required").each(function () {
            var name = $(this).attr("name");
            var val = $('input:radio[name="' + name + '"]:checked').val();
            if (typeof (val) == "undefined") {
                isOk = false;
                return false;
            }
        });
        return isOk;
    }

    function doSubmit(submitType) {
        var data = $("#form-kaoping").serializeArray();
        data.push({"name": "submitType", "value": submitType});
        //注意：所有领导评分在evaluation_result表中以field=target存储
        data.push({"name": "tableDataJson", "value": JSON.stringify($("#bootstrap-table").bootstrapTable('getData'))});
        /*if(submitType=="TJ"){
            if(!$.rule.checkTableRequired()){
                $.modal.alertError("请将信息填写完整后提交!");
                return;
            }
        }*/
        if (!checkTableRequired()) {
            $.modal.alertError("请将信息填写完整后提交!");
            return;
        }
        $.operate.saveTab(ctx + "zzjx/approver/doSubmit", data);
    }

    function checkFile(obj) {
        var filePath = obj;
        debugger
        if (filePath != "") {
            var fileLastName = filePath.substring(filePath.lastIndexOf("\."), filePath.length);
            var strRegex = "(.xls|.xlsx|.ppt|.pptx|.pdf|.doc|.docx)$"; //用于验证扩展名的正则表达式
            var picRegex = "(.jpg|.png|.gif|.ps|.jpeg)$";
            var re = new RegExp(strRegex);
            var rePic = new RegExp(picRegex);
            if (re.test(fileLastName.toLowerCase())) {
                $.modal.loading("正在生成预览文件，请稍后...");
                $.ajax({
                    async: true,
                    type: "post",
                    url: ctx + "zzjx/projectSysfile/files2Pdf?filePath=" + filePath,
                    success: function (data) {
                        //layer.msg('操作成功！');
                        console.log(data);
                        if (data.toString().indexOf("/") > 0) {
                            var filePath = data.substring(data.toString().indexOf("profile/"), data.length);
                            window.open(ctx + "js/web/viewer.html?file=" + ctx + filePath);
                        }
                        $.modal.closeLoading();
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        layer.msg('操作失败！');
                        $.modal.closeLoading();
                    }
                });
            } else if (rePic.test(fileLastName.toLowerCase())) {
                var filePath2 = filePath.substring(filePath.toString().indexOf("profile/"), filePath.length);
                window.open(ctx + filePath2);
            } else {
                $.modal.confirm("该文件不可进行预览!是否下载?", function () {
                    downloadFile(obj);
                });
            }
        }
    }

    function appealfun() {
        var approverId = $("#id").val();
        var roleId = $("#roleId").val();
        $.modal.loading("正在加载数据，请稍后...");
        var url = ctx + "zzjx/appealTab/toAppeal/" + approverId + "/" + roleId;
        debugger
        var _url = $.common.isEmpty(url) ? "/404.html" : url;
        var _height = ($(window).height() - 50);
        layer.open({
            type: 2,
            maxmin: true,
            shade: 0.3,
            title: '申诉管理',
            fix: false,
            area: [1199 + 'px', _height + 'px'],
            content: _url,
            shadeClose: false,
            btn: ['<i class="fa fa-close"></i> 关闭'],
            cancel: function () {
                return true;
            }
        });
        $.modal.closeLoading();
    }
</script>
</body>
</html>